package com.accelstack.cmp.controller;

import com.accelstack.cmp.entity.Datacenter;
import com.accelstack.cmp.service.DatacenterService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 数据中心控制器
 */
@Tag(name = "数据中心管理", description = "数据中心的增删改查接口")
@RestController
@RequestMapping("/api/datacenters")
@RequiredArgsConstructor
public class DatacenterController {

    private final DatacenterService datacenterService;
    
    @Operation(summary = "创建数据中心", description = "创建一个新的数据中心")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "创建成功"),
            @ApiResponse(responseCode = "400", description = "请求参数错误")
    })
    @PostMapping
    public ResponseEntity<Datacenter> create(@RequestBody Datacenter datacenter) {
        return ResponseEntity.ok(datacenterService.create(datacenter));
    }

    @Operation(summary = "更新数据中心", description = "更新现有数据中心的信息")
    @PutMapping("/{id}")
    public ResponseEntity<Datacenter> update(
            @Parameter(description = "数据中心ID", required = true) @PathVariable Long id,
            @RequestBody Datacenter datacenter) {
        datacenter.setId(id);
        return ResponseEntity.ok(datacenterService.update(datacenter));
    }

    @Operation(summary = "删除数据中心", description = "根据ID删除数据中心")
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> delete(
            @Parameter(description = "数据中心ID", required = true) @PathVariable Long id) {
        datacenterService.deleteById(id);
        return ResponseEntity.ok().build();
    }

    @Operation(summary = "查询数据中心详情", description = "根据ID查询数据中心的详细信息")
    @GetMapping("/{id}")
    public ResponseEntity<Datacenter> getById(
            @Parameter(description = "数据中心ID", required = true) @PathVariable Long id) {
        return datacenterService.findById(id)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    @Operation(summary = "查询所有数据中心", description = "查询系统中所有的数据中心列表")
    @GetMapping
    public ResponseEntity<List<Datacenter>> getAll() {
        return ResponseEntity.ok(datacenterService.findAll());
    }

    @Operation(summary = "分页查询数据中心", description = "分页查询数据中心列表")
    @GetMapping("/page")
    public ResponseEntity<Page<Datacenter>> getPage(
            @PageableDefault(size = 10, sort = "id") Pageable pageable) {
        return ResponseEntity.ok(datacenterService.findAll(pageable));
    }
}

